package gui;

import gui.events.EingabeEvent;
import gui.interfaces.EingabeListener;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.EventListenerList;

import datatypes.Stadt;

public class EingabePanel extends JPanel {

	private static final long 	serialVersionUID = 7189604117885253446L;
	private EventListenerList 	listenerList = new EventListenerList();
	
	private ArrayList<Stadt>	cities;
	
	JButton 					selectButton;
	JButton 					resetButton;
	JButton 					filterButton;
	
	JComboBox<Stadt> 			stadtlistenBox;
	JTextField 					stadtSuche;
	
	GridBagConstraints 			gc;
	

	public EingabePanel(){
		
		Dimension size = getPreferredSize();
		size.width= 500;
		size.height=70;
		setPreferredSize(size);
		setBorder(BorderFactory.createTitledBorder("Stadtauswahl"));
		setLayout(new GridBagLayout());
		
		initComponents();
		initListeners();
		
		initGridBag();
	}
	
	private void initComponents() {
		this.selectButton = new JButton("Select");
		this.resetButton = new JButton("Reset");
		this.filterButton = new JButton("Filter");
		
		this.stadtlistenBox = new JComboBox<Stadt>(Stadt.values());
		
		this.stadtSuche = new JTextField(10);
		
		this.gc = new GridBagConstraints();
		
		this.cities = new ArrayList<Stadt>();
		
		for (Stadt s : Stadt.values()) {
			cities.add(s);
		}
	}
	
	private void initListeners() {
		this.filterButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				String filterString = stadtSuche.getText();
				if (!filterString.isEmpty()) {
					ArrayList<Stadt> gefilterteListe = doFilter(filterString);
					getStadtlistenBox().removeAllItems();
					for (Stadt stadt : gefilterteListe) {
						getStadtlistenBox().addItem(stadt);
					}
				} else {
					resetCityListBox();
				}
			}
		});
		
		this.selectButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				Stadt stadt = (Stadt) stadtlistenBox.getSelectedItem();
//				System.out.println("selected Item: "+s);
//				System.out.println("selected Item: "+s.toString());
				
				fireEingabeEvent(new EingabeEvent(this, stadt));
			}
		});
		
		this.resetButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				stadtSuche.setText("");
				resetCityListBox();
			}
		});
	}
	
	private void initGridBag(){
		gc.anchor = GridBagConstraints.LINE_START;
		gc.weightx = 1;
		gc.weighty = 1;
		
		gc.gridx = 0;
		gc.gridy = 0;
		add(stadtSuche, gc);
		
		gc.gridx = 1;
		gc.gridy = 0;
		add(filterButton, gc);
		
		gc.gridx = 2;
		gc.gridy = 0;
		add(stadtlistenBox, gc);
		
		gc.gridx = 3;
		gc.gridy = 0;
		add(selectButton, gc);
		
		gc.gridx = 4;
		gc.gridy = 0;
		add(resetButton, gc);
	}
	
	private ArrayList<Stadt> doFilter(String s){
		ArrayList<Stadt> filteredArray = new ArrayList<Stadt>();
		
		for (Stadt stadt : this.cities) {
			if (stadt.toString().toLowerCase().contains(s.toLowerCase())) {
				filteredArray.add(stadt);
			}
		}
		
		return filteredArray;
	}
	
	public void fireEingabeEvent(EingabeEvent event){
		Object[] listeners = this.listenerList.getListenerList();
		
		for (int i = 0; i < listeners.length; i += 2) {
			if (listeners[i] == EingabeListener.class) {
				((EingabeListener)listeners[i+1]).eingabeEventOccurred(event);
			}
		}
	}

	public void addEingabeListener(EingabeListener listener){
		this.listenerList.add(EingabeListener.class, listener);
	}

	public void removeEingabeListener(EingabeListener listener){
		this.listenerList.remove(EingabeListener.class, listener);
	}

	private void resetCityListBox(){
		getStadtlistenBox().removeAllItems();
		for (Stadt stadt : Stadt.values()) {
			getStadtlistenBox().addItem(stadt);
		}
	}
	
	public void resetEingabePanel(){
		this.stadtSuche.setText("");
		this.resetCityListBox();
	}

	public JComboBox<Stadt> getStadtlistenBox() {
		return stadtlistenBox;
	}
}